package rpg.Utils
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.net.LocalConnection;

	public class GameUtils
	{
		/**
		 * 人物图层的容器
		 */
		public static var stage : DisplayObject;
		/**
		 * 帧频率
		 */
		public static const frameRate : int = 30;
		/**
		 *地图的缩放比例
		 */
		public static const mapRate : int = 10;
		/**
		 *地图的坐标
		 */
		public static var viewX : int = 0;
		public static var viewY : int = 0;
		/**
		 *屏幕大小
		 */
		public static var stageWidth : int;
		public static var stageHeight : int;
		/**
		 *全图大小
		 */
		public static var sceneWidth : int;
		public static var sceneHeight : int;
		/**
		 *地图格子快大小
		 */
		public static const GRID_WIDTH : int = 50;
		public static const GRID_HEIGHT : int = 50;

		public static function gc() : void
		{
			try
			{
				new LocalConnection().connect("gc");
				new LocalConnection().connect("gc");
			}
			catch(e : *)
			{
				Log.print("强制调用GC");
			}
		}

		public static function getIntPixelX(pixelX : int) : int
		{
			return getPixelXByGrid(getGridXByPixel(pixelX));
		}

		public static function getIntPixelY(pixelY : int) : int
		{
			return getPixelYByGrid(getGridYByPixel(pixelY));
		}

		public static function getPixelXByGrid(gridX : int) : int
		{
			return gridX * GRID_WIDTH + GRID_WIDTH / 2;
		}

		public static function getPixelYByGrid(gridY : int) : int
		{
			return gridY * GRID_HEIGHT + GRID_HEIGHT / 2;
		}

		public static function getGridXByPixel(pixelX : int) : int
		{
			return int(pixelX / GRID_WIDTH);
		}
		
		/**
		 * 根据弧度得到角度
		 * @param rotate
		 * @return
		 */
		public static function getAngleByRotate(rotate : Number) : int
		{
			return Math.round(rotate * 180 / Math.PI);
		}

		public static function getGridYByPixel(pixelY : int) : int
		{
			return int(pixelY / GRID_HEIGHT);
		}

		public static function getGridByPixel(pixelX : int , pixelY : int) : Array
		{
			var grid : Array = [];
			grid[0] = getGridXByPixel(pixelX);
			grid[1] = getGridYByPixel(pixelY);
			return grid;
		}

		public static function getStepTime(speed : Number) : int
		{
			return int(GRID_WIDTH / speed);
		}

		public static function getSpeed(stepTime : int) : Number
		{
			return Number((GRID_WIDTH / stepTime).toFixed(3));
		}

		public static function getAttackRange(grids : int) : int
		{
			return grids * GRID_WIDTH;
		}

		public static function checkRoleIsInView(screenX : int , screenY : int , width : int , height : int) : Boolean
		{
			if((screenX >= -width && screenX <= stageWidth + width && screenY >= -height && screenY <= stageHeight + height))
			{
				return true;
			}
			return false;
		}


		/**
		 * 计算两点距离
		 * @param Ax	A点x坐标
		 * @param Ay	A点y坐标
		 * @param Bx	B点x坐标
		 * @param By	B点y坐标
		 */
		public static function getDistance(Ax : int , Ay : int , Bx : int , By : int) : int
		{
			return (Math.sqrt(((Ax - Bx) * (Ax - Bx)) + ((Ay - By) * (Ay - By))));
		}

		/**
		 * 计算亮点之间的角度
		 * @param Ax	A点x坐标
		 * @param Ay	A点y坐标
		 * @param Bx	B点x坐标
		 * @param By	B点y坐标
		 */
		public static function getRotation(Ax : int , Ay : int , Bx : int , By : int) : int
		{
			var tempXDistance : int = Bx - Ax;
			var tempYDistance : int = By - Ay;

			var rotation : int = Math.round(Math.atan2(tempYDistance , tempXDistance) * 57.33);
			rotation = (rotation + 360) % 360;

			return rotation;
		}

		/**
		 * 取得一个方向的反向
		 * @param dir
		 * @return
		 *
		 */
		public static function getReverseDirByDir(dir : int) : int
		{
			switch(dir)
			{
				case Val.EAST:
					return Val.WEST;
				case Val.EAST_SOUTH:
					return Val.WEST_NORTH;
				case Val.SOUTH:
					return Val.NORTH;
				case Val.EAST_NORTH:
					return Val.WEST_SOUTH;
				case Val.NORTH:
					return Val.SOUTH;
				case Val.WEST:
					return Val.EAST;
				case Val.WEST_NORTH:
					return Val.EAST_SOUTH;
				case Val.WEST_SOUTH:
					return Val.EAST_NORTH;
					break;
			}
			return -1;
		}

		public static function getRotationByDir(dir : int) : int
		{
			if(dir == Val.SOUTH)
			{
				return 90;
			}
			else if(dir == Val.WEST)
			{
				return 180;
			}
			else if(dir == Val.EAST)
			{
				return 0;
			}
			else if(dir == Val.NORTH)
			{
				return 270;
			}
			else if(dir == Val.WEST_SOUTH)
			{
				return 135;
			}
			else if(dir == Val.EAST_SOUTH)
			{
				return 45;
			}
			else if(dir == Val.WEST_NORTH)
			{
				return 225;
			}
			else if(dir == Val.EAST_NORTH)
			{
				return 315;
			}
			return 0;
		}

		/**
		 * 根据角度获取方向
		 * @param rotation
		 * @return
		 */
		public static function getDirection(rotation : int) : int
		{
			if(rotation <= 22 || rotation >= 338)
			{
				return Val.EAST;
			}

			if(rotation >= 23 && rotation <= 67)
			{
				return Val.EAST_SOUTH;
			}

			if(rotation >= 68 && rotation <= 112)
			{
				return Val.SOUTH;
			}

			if(rotation >= 113 && rotation <= 157)
			{
				return Val.WEST_SOUTH;
			}

			if(rotation >= 158 && rotation <= 202)
			{
				return Val.WEST;
			}

			if(rotation >= 203 && rotation <= 247)
			{
				return Val.WEST_NORTH;
			}

			if(rotation >= 248 && rotation <= 292)
			{
				return Val.NORTH;
			}

			if(rotation >= 293 && rotation <= 337)
			{
				return Val.EAST_NORTH;
			}

			return -1;
		}

		/**
		 * 以角度为单位计算三角函数值
		 * @param angle
		 * @return
		 *
		 */
		public static function sinD(angle : Number) : Number
		{
			return Math.sin(getAngleByRotation(angle));
		}

		public static function cosD(angle : Number) : Number
		{
			return Math.cos(getAngleByRotation(angle));
		}

		public static function tanD(angle : Number) : Number
		{
			return Math.tan(getAngleByRotation(angle));
		}

		public static function getAngleByRotation(rotate : Number) : Number
		{
			return rotate * (Math.PI / 180);
		}

		public static function getRotationByAngle(angle : Number) : int
		{
			return Math.round(angle * 180 / Math.PI);
		}
//		public static function getFlyTime(startX : int , startY : int , targetX : int , targetY : int) : int
//		{
//			var dist : int = CommonUtil.getDistance(startX , startY , targetX , targetY);
//			if(dist > 200)
//				return 600;
//			else if(dist > 150)
//				return 400;
//			return dist < 100 ? 100 : 200;
//		}

	}
}